package restaurant.behaviours.waiter;

import restaurant.agents.WaiterAgent;
import restaurant.core.CustomizedPerformative;
import jade.core.behaviours.CyclicBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.lang.acl.ACLMessage;

public class WaiterSyncroBehaviour extends CyclicBehaviour {
	
	private static final long serialVersionUID = -2146129084297513558L;
	private WaiterAgent agent;
	
	public WaiterSyncroBehaviour(WaiterAgent agent) {
		this.agent = agent;
	}

	@Override
	public void action() {
		DFAgentDescription agentDescription = new DFAgentDescription();
		ServiceDescription serviceDescription = new ServiceDescription();
		serviceDescription.setType("waiters-management");
		agentDescription.addServices(serviceDescription);
	
		try {
			agentDescription = DFService.search(myAgent, agentDescription)[0];
		} catch (FIPAException e) {
			e.printStackTrace();
		}
		ACLMessage message = new ACLMessage(CustomizedPerformative.SYNCRO);
		if(this.agent.isFree()) {
			message.setContent("YES");
		} else {
			message.setContent("NO");
		}
		message.addReceiver(agentDescription.getName());
		myAgent.send(message);
	}

}